package com.ganxiang.ximenhong.widght;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部的Tabbar
 */
public class TabBarView extends LinearLayout{
	private TabBarAdapter mAdapter;
	private boolean mIsFirstCreate = true, mGetFocusSelectable = true;
	private final int none_position = 1000;
	private int mCurrentPosition = none_position;
	private OnItemSelectedListener mOnItemSelectedListener;
	private OnItemClickListener mOnItemClickListener;
	private List<View> mViews = new ArrayList<View>();
	private int mKeyCode = 0;
	private boolean mIsGetFouse = true;
	
	public interface TabBarAdapter{
		int getCount();
		View getOnView(int position, View convertView);
		View getOffView(int position, View convertView);
		void onFocus(View v, boolean hasFocus);
	}
	
	public interface OnItemSelectedListener{
		void onItemSelected(int position);
	}
	
	public interface OnItemClickListener{
		void onItemClick(int position);
	}
 
	public void setIsGetFouse(boolean bl){
		mIsGetFouse = bl;
	}
	
	public TabBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public TabBarView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener){
		mOnItemSelectedListener = onItemSelectedListener;
	}
	
	public void setOnItemClickListener(OnItemClickListener onItemClickListener){
		mOnItemClickListener = onItemClickListener;
	}
	
	public void setAdapter(TabBarAdapter adapter){
		mAdapter = adapter;
		createUI();
	}

    public void clickItem(int position){
        if(mCurrentPosition == position) {
            mCurrentPosition = none_position;
        }
        selectItem(position, true);
    }

    public void clickItem(int position, boolean isResponse){
        if(mCurrentPosition == position) {
            mCurrentPosition = none_position;
        }
        selectItem(position, isResponse);
    }

    public void selectItem(final int position, boolean isResponse){
    	if(position < 0){
    		if(mCurrentPosition != none_position){
            	mAdapter.getOffView(mCurrentPosition, getChildAt(mCurrentPosition));
            	mCurrentPosition = none_position;
            }
    		return;
    	}
        if(mCurrentPosition != position){
            View v = getChildAt(position);
            v.setId(position);
            mAdapter.getOnView(position, v);
            if(mCurrentPosition != none_position){
            	mAdapter.getOffView(mCurrentPosition, getChildAt(mCurrentPosition));
            }
            mCurrentPosition = position;
            if(mOnItemSelectedListener != null && isResponse){
                mOnItemSelectedListener.onItemSelected(position);
            }
        }
    }

	public void selectItem(int position){
		selectItem(position, true);
	}
	
	public int getCurrentPosition(){
		return mCurrentPosition;
	}

	public void cancleSelected(){
		mAdapter.getOffView(mCurrentPosition, getChildAt(mCurrentPosition));
		mCurrentPosition = 1000;
	}
	
	public void setSelectIcon(int lastIndex){
		mCurrentPosition = lastIndex;
		mAdapter.getOnView(mCurrentPosition, getChildAt(mCurrentPosition));
	}
	
	//public
	
	public void setGetFocusSelectable(boolean able){
		mGetFocusSelectable = able;
	}
	
	public View getItem(int position){
		return getChildAt(position);
	}
	
	private void createUI(){
		if(mIsFirstCreate){
			setOrientation(LinearLayout.HORIZONTAL);
			for(int position = 0; position < mAdapter.getCount(); position++){
				View v = mAdapter.getOffView(position, null);
				addView(v, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT, 1));
				v.setId(position);
				v.setOnClickListener(new ItemClickListener());
				v.setOnFocusChangeListener(new OnFocusChangeListener() {
					
					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						System.out.println("setOnFocusChangeListener==="+hasFocus+" "+v.getId());
						if(!mIsGetFouse) return;
						if(hasFocus){
							if(mKeyCode == KeyEvent.KEYCODE_DPAD_UP){
								if(v.getId() != getCurrentPosition()){
									getChildAt(getCurrentPosition()).requestFocus();
									mAdapter.onFocus(v, false);
								}
								else{
									mAdapter.onFocus(v, true);
								}
							}
							else{
								if(mGetFocusSelectable){						
									selectItem(v.getId(), true);
									
								}
								mAdapter.onFocus(v, true);
							}
						}
						else mAdapter.onFocus(v, hasFocus);
					}
				});
			}
			mIsFirstCreate = false;
		}
	}
	
	public boolean isNoFocus(){
		for(int i = 0; i < getChildCount(); i++){
			return getChildAt(i).hasFocus();
		}
		return false;
	}
	
	class ItemClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int position = v.getId();		
			selectItem(position, true);
			if(mOnItemClickListener != null){
				mOnItemClickListener.onItemClick(position);
			}
		}
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}
	
	public void listenTVEvent(int keyCode, KeyEvent event){
		mKeyCode = keyCode;
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:			
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			break;
		case KeyEvent.KEYCODE_ENTER:
		case KeyEvent.KEYCODE_DPAD_CENTER:
			break;
		}
	}

}
